package com.yyj.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yyj.domain.Goods;
import com.yyj.service.GoodsService;

/**
 * 用户控制器类
 */
@Controller
public class MarangeController {
	// 依赖注入
	@Autowired
	private GoodsService goodsService;

	/*
	 * 商品列表列表
	 */
	@RequestMapping(value = "/management.do")
	public ModelAndView getAllGoods() {
		ModelAndView mav = new ModelAndView("management");
		List<Goods> goods = goodsService.getAllGoods();
		mav.addObject("goods", goods);
		return mav;
	}

	/**
	 * 创建客户
	 */
	@RequestMapping("/managementcreate.do")
	@ResponseBody
	public String managementCreate(Goods goods) {
		// 执行Service层中的创建方法，返回的是受影响的行数
		int rows = goodsService.createGoods(goods);
		System.out.println(goods);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	/**
	 * 通过id获取客户信息
	 */
	@RequestMapping("/getManagementById.do")
	@ResponseBody
	public Goods getGoodsById(Integer goods_id) {
		Goods goods = goodsService.getGoods(goods_id);
		return goods;
	}

	/**
	 * 更新客户
	 */
	@RequestMapping("/managementupdate.do")
	@ResponseBody
	public String goodsUpdate(Goods goods) {
		int rows = goodsService.updateGoods(goods);
		System.out.println(goods);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	/**
	 * 删除客户
	 */
	@RequestMapping("/managementdelete.do")
	@ResponseBody
	public String managementDelete(Integer goods_id) {
		int rows = goodsService.deleteGoods(goods_id);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

}
